/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

//import java.sql.*;
//import java.sql.SQLException;
//import java.util.ArrayList;
import java.util.List;
import org.hibernate.*;
import pojo.Sanpham;
import util.HibernateUtil;



/**
 *
 * @author NguyenMinhTan
 */
public class SanPhamDao {

    public static List<Sanpham> SelectAllSanpham() {
        List<Sanpham> lsSanpham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Sanpham sp where sp.tinhtrang=:tinhTrang order by sp.id asc");
        query.setInteger("tinhTrang", 1);
        lsSanpham = query.list();
        session.close();
        return lsSanpham;
    }

    public static Sanpham layThongTinSanPham(int masp) {
        Sanpham sp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        sp = (Sanpham) session.get(Sanpham.class, masp);
        return sp;
    }

    public static boolean InsertSanPham(Sanpham sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (SanPhamDao.layThongTinSanPham(sp.getIdsp()) != null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = (Transaction) session.beginTransaction();
            session.save(sp);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            kq = false;
        } finally {
            session.close();
        }

        return kq;
    }

    public static void DeleteSanPham(Sanpham sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        sp.setTinhtrang(0);
        session.update(sp);
        Transaction tx = session.beginTransaction();
        tx.commit();
        session.close();

    }

    public static void UpdateSanPham(Sanpham sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        transaction = (Transaction) session.beginTransaction();
        session.update(sp);
        transaction.commit();
        session.close();

    }
    
    public static List<Sanpham> getListSanPham(int maCuaHang, int maLoaiSP){
        List<Sanpham> ds;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;        
        transaction = (Transaction) session.beginTransaction();
        Query query = session.createQuery("Select sp from Sanpham as sp inner join sp.loaisp as loai inner join sp.chitietsanphams as chitiet where loai.idloai=:maLoai and chitiet.cuahang=:maCH");
        query.setInteger("maLoai", maLoaiSP);
        query.setInteger("maCH", maCuaHang);
        transaction.commit();
        ds = query.list();
        session.close();
        return ds;
    }
    
    
}
